<?php

namespace app\admin\model\yunfood;

use addons\yunfood\model\OrderItem;

/**
 * 退款订单
 */
class OrderRefund extends \addons\yunfood\model\OrderRefund
{
    // 自定义初始化
    protected static function init()
    {
        self::beforeWrite(function ($record) {
            $scenario = $record->data['scenario'] ?? null;
            if ($scenario === 'reply') {
                if ($record->status == self::STATUS_WAIT) {
                    $agree = $record['agree'] ?? 1;
                    if ($agree) {
                        if ($record->refund_amount_rel > 0 && $record->refund_way == OrderRefund::REFUND_WAY_ORIGIN) {
                            $record->refundment_status = self::REFUNDMENT_STATUS_WAIT;
                            $record->status = self::STATUS_ING;
                        } else {
                            $record->status = self::STATUS_DONE;
                        }
                    } else {
                        $record->refund_balance_rel = 0;
                        $record->refund_amount_rel = 0;
                        $record->status = self::STATUS_REFUSE;
                    }
                    $record->refund_time = time();

                    // 处理订单数据
                    self::handleOrderRefund($record, true);
                }
            }

            if (! empty($record->data['deleted_at']) && $record->status != self::STATUS_DONE) {
                $orderData = $record->orderData;
                if ($record->order_item_no) {
                    // 重置子订单退款
                    if ($orderItemData = $record->orderItemData) {
                        if ($orderItemData->refund_no == $record->refund_no) {
                            $orderItemData->refund_no = null;
                            $orderItemData->refund_status = OrderItem::REFUND_STATUS_NO;
                            $orderItemData->save();
                        }
                    }
                    // 重置订单退款
                    if ($orderData) {
                        $allRefundNo = true;
                        if ($orderItemList = $orderData->orderItemListData) {
                            foreach ($orderItemList as $item) {
                                if ($item->refund_status != OrderItem::REFUND_STATUS_NO) {
                                    $allRefundNo = false;
                                }
                            }
                        }
                        if ($allRefundNo) {
                            $orderData->refund_status = Order::REFUND_STATUS_NO;
                            $orderData->save();
                        }
                    }
                } else {
                    // 重置订单退款
                    if ($orderData && $orderData->refund_no == $record->refund_no) {
                        $orderData->refund_no = null;
                        $orderData->refund_status = Order::REFUND_STATUS_NO;
                        $orderData->save();
                    }
                }
            }
        });
    }
}
